

<!DOCTYPE html>
<html class="writer-html5" lang="zh" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>登录 &mdash; Scrapy 2.3.0 文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.custom.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.bundle.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-shadow.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-punk.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-noir.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-light.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-borderless.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/micromodal.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/sphinx_rtd_theme.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/js/hoverxref.js"></script>
        <script src="../_static/js/tooltipster.bundle.min.js"></script>
        <script src="../_static/js/micromodal.min.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="统计数据集合" href="stats.html" />
    <link rel="prev" title="例外情况" href="exceptions.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> Scrapy
          

          
          </a>

          
            
            
              <div class="version">
                2.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">第一步</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/overview.html">Scrapy一目了然</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/install.html">安装指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/tutorial.html">Scrapy 教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/examples.html">实例</a></li>
</ul>
<p class="caption"><span class="caption-text">基本概念</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="commands.html">命令行工具</a></li>
<li class="toctree-l1"><a class="reference internal" href="spiders.html">蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="selectors.html">选择器</a></li>
<li class="toctree-l1"><a class="reference internal" href="items.html">项目</a></li>
<li class="toctree-l1"><a class="reference internal" href="loaders.html">项目加载器</a></li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Scrapy shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="item-pipeline.html">项目管道</a></li>
<li class="toctree-l1"><a class="reference internal" href="feed-exports.html">Feed 导出</a></li>
<li class="toctree-l1"><a class="reference internal" href="request-response.html">请求和响应</a></li>
<li class="toctree-l1"><a class="reference internal" href="link-extractors.html">链接提取器</a></li>
<li class="toctree-l1"><a class="reference internal" href="settings.html">设置</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">例外情况</a></li>
</ul>
<p class="caption"><span class="caption-text">内置服务</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">登录</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#log-levels">日志级别</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-to-log-messages">如何记录消息</a></li>
<li class="toctree-l2"><a class="reference internal" href="#logging-from-spiders">从蜘蛛记录</a></li>
<li class="toctree-l2"><a class="reference internal" href="#logging-configuration">日志记录配置</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#logging-settings">日志记录设置</a></li>
<li class="toctree-l3"><a class="reference internal" href="#command-line-options">命令行选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="#custom-log-formats">自定义日志格式</a></li>
<li class="toctree-l3"><a class="reference internal" href="#advanced-customization">高级自定义</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#module-scrapy.utils.log">scrapy.utils.log模块</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="stats.html">统计数据集合</a></li>
<li class="toctree-l1"><a class="reference internal" href="email.html">发送电子邮件</a></li>
<li class="toctree-l1"><a class="reference internal" href="telnetconsole.html">远程登录控制台</a></li>
<li class="toctree-l1"><a class="reference internal" href="webservice.html">Web服务</a></li>
</ul>
<p class="caption"><span class="caption-text">解决具体问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
<li class="toctree-l1"><a class="reference internal" href="debug.html">调试spiders</a></li>
<li class="toctree-l1"><a class="reference internal" href="contracts.html">蜘蛛合约</a></li>
<li class="toctree-l1"><a class="reference internal" href="practices.html">常用做法</a></li>
<li class="toctree-l1"><a class="reference internal" href="broad-crawls.html">宽爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer-tools.html">使用浏览器的开发人员工具进行抓取</a></li>
<li class="toctree-l1"><a class="reference internal" href="dynamic-content.html">选择动态加载的内容</a></li>
<li class="toctree-l1"><a class="reference internal" href="leaks.html">调试内存泄漏</a></li>
<li class="toctree-l1"><a class="reference internal" href="media-pipeline.html">下载和处理文件和图像</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">部署蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="autothrottle.html">AutoThrottle 扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmarking.html">标杆管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="jobs.html">作业：暂停和恢复爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="coroutines.html">协同程序</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio.html">asyncio</a></li>
</ul>
<p class="caption"><span class="caption-text">扩展Scrapy</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="architecture.html">体系结构概述</a></li>
<li class="toctree-l1"><a class="reference internal" href="downloader-middleware.html">下载器中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="spider-middleware.html">蜘蛛中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">核心API</a></li>
<li class="toctree-l1"><a class="reference internal" href="signals.html">信号</a></li>
<li class="toctree-l1"><a class="reference internal" href="exporters.html">条目导出器</a></li>
</ul>
<p class="caption"><span class="caption-text">其余所有</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../news.html">发行说明</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">为 Scrapy 贡献</a></li>
<li class="toctree-l1"><a class="reference internal" href="../versioning.html">版本控制和API稳定性</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Scrapy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>登录</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="logging">
<span id="topics-logging"></span><h1>登录<a class="headerlink" href="#logging" title="永久链接至标题">¶</a></h1>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">scrapy.log</span></code> 已经不赞成与函数一起使用，而赞成显式调用Python标准日志记录。继续阅读以了解有关新日志记录系统的更多信息。</p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-1466963416408457"
     data-ad-slot="8850786025"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<p>零星用途 <a class="reference external" href="https://docs.python.org/3/library/logging.html#module-logging" title="(在 Python v3.9)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> 用于事件日志记录。我们将提供一些简单的示例来帮助您入门，但对于更高级的用例，强烈建议您仔细阅读其文档。</p>
<p>日志记录是开箱即用的，可以在某种程度上使用中列出的  Scrapy   设置进行配置。 <a class="reference internal" href="#topics-logging-settings"><span class="std std-ref">日志记录设置</span></a> .</p>
<p>Scrapy电话 <code class="xref py py-func docutils literal notranslate"><span class="pre">scrapy.utils.log.configure_logging()</span></code> 设置一些合理的默认值并在 <a class="reference internal" href="#topics-logging-settings"><span class="std std-ref">日志记录设置</span></a> 当运行命令时，建议在运行脚本的scrapy时手动调用它，如中所述。 <a class="reference internal" href="practices.html#run-from-script"><span class="std std-ref">从脚本中运行Scrapy</span></a> .</p>
<div class="section" id="log-levels">
<span id="topics-logging-levels"></span><h2>日志级别<a class="headerlink" href="#log-levels" title="永久链接至标题">¶</a></h2>
<p>python的内置日志记录定义了5个不同的级别，以指示给定日志消息的严重性。以下是标准的，按降序排列：</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">logging.CRITICAL</span></code> -对于严重错误（严重性最高）</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">logging.ERROR</span></code> -对于常规错误</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">logging.WARNING</span></code> -用于警告消息</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">logging.INFO</span></code> -以获取信息性消息</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code> -用于调试消息（最低严重性）</p></li>
</ol>
</div>
<div class="section" id="how-to-log-messages">
<h2>如何记录消息<a class="headerlink" href="#how-to-log-messages" title="永久链接至标题">¶</a></h2>
<p>下面是如何使用 <code class="docutils literal notranslate"><span class="pre">logging.WARNING</span></code></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;This is a warning&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>在标准的5个级别中，有一个用于发布日志消息的快捷方式，还有一个常规的 <code class="docutils literal notranslate"><span class="pre">logging.log</span></code> 方法，该方法将给定的级别作为参数。如果需要，最后一个示例可以重写为：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">WARNING</span><span class="p">,</span> <span class="s2">&quot;This is a warning&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>除此之外，您还可以创建不同的“记录器”来封装消息。（例如，常见的做法是为每个模块创建不同的记录器）。这些记录器可以独立配置，并且允许层次结构。</p>
<p>前面的示例在后台使用根记录器，它是一个顶级记录器，所有消息都在其中传播（除非另有规定）。使用 <code class="docutils literal notranslate"><span class="pre">logging</span></code> 帮助程序只是显式获取根记录器的快捷方式，因此这也相当于最后一段代码：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;This is a warning&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>您可以使用不同的记录器，只需将其名称 <code class="docutils literal notranslate"><span class="pre">logging.getLogger</span></code> 功能：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;mycustomlogger&#39;</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;This is a warning&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>最后，通过使用 <code class="docutils literal notranslate"><span class="pre">__name__</span></code> 变量，用当前模块的路径填充：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;This is a warning&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt>模块日志记录， <a class="reference external" href="https://docs.python.org/3/howto/logging.html" title="(在 Python v3.9)"><span class="xref std std-doc">HowTo</span></a></dt><dd><p>基本日志教程</p>
</dd>
<dt>模块日志记录， <a class="reference external" href="https://docs.python.org/3/library/logging.html#logger" title="(在 Python v3.9)"><span class="xref std std-ref">Loggers</span></a></dt><dd><p>关于伐木工人的进一步文件</p>
</dd>
</dl>
</div>
</div>
<div class="section" id="logging-from-spiders">
<span id="topics-logging-from-spiders"></span><h2>从蜘蛛记录<a class="headerlink" href="#logging-from-spiders" title="永久链接至标题">¶</a></h2>
<p>Scrapy提供了 <a class="reference internal" href="spiders.html#scrapy.spiders.Spider.logger" title="scrapy.spiders.Spider.logger"><code class="xref py py-data docutils literal notranslate"><span class="pre">logger</span></code></a> 在每个蜘蛛实例中，可以这样访问和使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">scrapy</span>

<span class="k">class</span> <span class="nc">MySpider</span><span class="p">(</span><span class="n">scrapy</span><span class="o">.</span><span class="n">Spider</span><span class="p">):</span>

    <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;myspider&#39;</span>
    <span class="n">start_urls</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;https://scrapinghub.com&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Parse function called on </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
</pre></div>
</div>
<p>这个记录器是使用蜘蛛的名称创建的，但是您可以使用任何您想要的自定义Python记录器。例如：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">scrapy</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;mycustomlogger&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">MySpider</span><span class="p">(</span><span class="n">scrapy</span><span class="o">.</span><span class="n">Spider</span><span class="p">):</span>

    <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;myspider&#39;</span>
    <span class="n">start_urls</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;https://scrapinghub.com&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">):</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Parse function called on </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="logging-configuration">
<span id="topics-logging-configuration"></span><h2>日志记录配置<a class="headerlink" href="#logging-configuration" title="永久链接至标题">¶</a></h2>
<p>日志记录者自己不管理如何显示通过它们发送的消息。对于此任务，可以将不同的“处理程序”附加到任何记录器实例，它们将这些消息重定向到适当的目标，例如标准输出、文件、电子邮件等。</p>
<p>默认情况下，scrappy根据下面的设置为根记录器设置和配置处理程序。</p>
<div class="section" id="logging-settings">
<span id="topics-logging-settings"></span><h3>日志记录设置<a class="headerlink" href="#logging-settings" title="永久链接至标题">¶</a></h3>
<p>这些设置可用于配置日志记录：</p>
<ul class="simple">
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_FILE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FILE</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_ENABLED"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_ENABLED</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_ENCODING"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_ENCODING</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_LEVEL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_LEVEL</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FORMAT</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_DATEFORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_DATEFORMAT</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_STDOUT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_STDOUT</span></code></a></p></li>
<li><p><a class="reference internal" href="settings.html#std-setting-LOG_SHORT_NAMES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_SHORT_NAMES</span></code></a></p></li>
</ul>
<p>前两个设置定义日志消息的目标。如果 <a class="reference internal" href="settings.html#std-setting-LOG_FILE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FILE</span></code></a> 设置后，通过根记录器发送的消息将被重定向到名为 <a class="reference internal" href="settings.html#std-setting-LOG_FILE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FILE</span></code></a> 带编码 <a class="reference internal" href="settings.html#std-setting-LOG_ENCODING"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_ENCODING</span></code></a> . 如果未设置 <a class="reference internal" href="settings.html#std-setting-LOG_ENABLED"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_ENABLED</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">True</span></code> ，将在标准错误上显示日志消息。最后，如果 <a class="reference internal" href="settings.html#std-setting-LOG_ENABLED"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_ENABLED</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">False</span></code> ，将不会有任何可见的日志输出。</p>
<p><a class="reference internal" href="settings.html#std-setting-LOG_LEVEL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_LEVEL</span></code></a> 确定要显示的最低严重性级别，将筛选出严重性较低的消息。它的范围包括 <a class="reference internal" href="#topics-logging-levels"><span class="std std-ref">日志级别</span></a> .</p>
<p><a class="reference internal" href="settings.html#std-setting-LOG_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FORMAT</span></code></a> 和 <a class="reference internal" href="settings.html#std-setting-LOG_DATEFORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_DATEFORMAT</span></code></a> 指定用作所有消息布局的格式字符串。这些字符串可以包含中列出的任何占位符 <a class="reference external" href="https://docs.python.org/3/library/logging.html#logrecord-attributes" title="(在 Python v3.9)"><span class="xref std std-ref">logging's logrecord attributes docs</span></a> 和 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior" title="(在 Python v3.9)"><span class="xref std std-ref">datetime's strftime and strptime directives</span></a> 分别。</p>
<p>如果 <a class="reference internal" href="settings.html#std-setting-LOG_SHORT_NAMES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_SHORT_NAMES</span></code></a> 则日志将不显示打印日志的废组件。默认情况下，它是未设置的，因此日志包含负责该日志输出的废弃组件。</p>
</div>
<div class="section" id="command-line-options">
<h3>命令行选项<a class="headerlink" href="#command-line-options" title="永久链接至标题">¶</a></h3>
<p>有一些命令行参数可用于所有命令，您可以使用这些参数来覆盖有关日志记录的一些零碎设置。</p>
<ul class="simple">
<li><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">--logfile</span> <span class="pre">FILE</span></code></dt><dd><p>重写 <a class="reference internal" href="settings.html#std-setting-LOG_FILE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FILE</span></code></a></p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">--loglevel/-L</span> <span class="pre">LEVEL</span></code></dt><dd><p>重写 <a class="reference internal" href="settings.html#std-setting-LOG_LEVEL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_LEVEL</span></code></a></p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">--nolog</span></code></dt><dd><p>集合 <a class="reference internal" href="settings.html#std-setting-LOG_ENABLED"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_ENABLED</span></code></a> 到 <code class="docutils literal notranslate"><span class="pre">False</span></code></p>
</dd>
</dl>
</li>
</ul>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt>模块 <a class="reference external" href="https://docs.python.org/3/library/logging.handlers.html#module-logging.handlers" title="(在 Python v3.9)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging.handlers</span></code></a></dt><dd><p>有关可用处理程序的进一步文档</p>
</dd>
</dl>
</div>
</div>
<div class="section" id="custom-log-formats">
<span id="id1"></span><h3>自定义日志格式<a class="headerlink" href="#custom-log-formats" title="永久链接至标题">¶</a></h3>
<p>可以通过扩展为不同的操作设置自定义日志格式 <a class="reference internal" href="#scrapy.logformatter.LogFormatter" title="scrapy.logformatter.LogFormatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogFormatter</span></code></a> 课程和制作 <a class="reference internal" href="settings.html#std-setting-LOG_FORMATTER"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_FORMATTER</span></code></a> 指向你的新班级。</p>
<dl class="py class">
<dt id="scrapy.logformatter.LogFormatter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.logformatter.</code><code class="sig-name descname">LogFormatter</code><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter" title="永久链接至目标">¶</a></dt>
<dd><p>用于为不同操作生成日志消息的类。</p>
<p>所有方法都必须返回一个列出参数的字典 <code class="docutils literal notranslate"><span class="pre">level</span></code> ， <code class="docutils literal notranslate"><span class="pre">msg</span></code> 和 <code class="docutils literal notranslate"><span class="pre">args</span></code> 调用时将用于构造日志消息 <code class="docutils literal notranslate"><span class="pre">logging.log</span></code> .</p>
<p>方法输出的字典键：</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">level</span></code> 是该操作的日志级别，可以使用 <a class="reference external" href="https://docs.python.org/3/library/logging.html">python logging library</a> ： <code class="docutils literal notranslate"><span class="pre">logging.DEBUG</span></code> ， <code class="docutils literal notranslate"><span class="pre">logging.INFO</span></code> ， <code class="docutils literal notranslate"><span class="pre">logging.WARNING</span></code> ， <code class="docutils literal notranslate"><span class="pre">logging.ERROR</span></code> 和 <code class="docutils literal notranslate"><span class="pre">logging.CRITICAL</span></code> .</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">msg</span></code> 应该是可以包含不同格式占位符的字符串。此字符串，格式为 <code class="docutils literal notranslate"><span class="pre">args</span></code> ，将是该行动的长期信息。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">args</span></code> 应该是一个tuple或dict，其中的格式占位符为 <code class="docutils literal notranslate"><span class="pre">msg</span></code> .最后的日志消息计算为 <code class="docutils literal notranslate"><span class="pre">msg</span> <span class="pre">%</span> <span class="pre">args</span></code> .</p></li>
</ul>
<p>用户可以定义自己的 <code class="docutils literal notranslate"><span class="pre">LogFormatter</span></code> 如果要自定义每个操作的日志记录方式，或者希望完全忽略它，则初始化。为了省略记录操作，该方法必须返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> .</p>
<p>以下是有关如何创建自定义日志格式化程序以降低从管道中删除项时日志消息的严重性级别的示例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PoliteLogFormatter</span><span class="p">(</span><span class="n">logformatter</span><span class="o">.</span><span class="n">LogFormatter</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">dropped</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">exception</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">spider</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">,</span> <span class="c1"># lowering the level from logging.WARNING</span>
            <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="s2">&quot;Dropped: </span><span class="si">%(exception)s</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">linesep</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="si">%(item)s</span><span class="s2">&quot;</span><span class="p">,</span>
            <span class="s1">&#39;args&#39;</span><span class="p">:</span> <span class="p">{</span>
                <span class="s1">&#39;exception&#39;</span><span class="p">:</span> <span class="n">exception</span><span class="p">,</span>
                <span class="s1">&#39;item&#39;</span><span class="p">:</span> <span class="n">item</span><span class="p">,</span>
            <span class="p">}</span>
        <span class="p">}</span>
</pre></div>
</div>
<dl class="py method">
<dt id="scrapy.logformatter.LogFormatter.crawled">
<code class="sig-name descname">crawled</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">request</span></em>, <em class="sig-param"><span class="n">response</span></em>, <em class="sig-param"><span class="n">spider</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter.crawled"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter.crawled" title="永久链接至目标">¶</a></dt>
<dd><p>当爬虫找到网页时记录一条消息。</p>
</dd></dl>

<dl class="py method">
<dt id="scrapy.logformatter.LogFormatter.download_error">
<code class="sig-name descname">download_error</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">failure</span></em>, <em class="sig-param"><span class="n">request</span></em>, <em class="sig-param"><span class="n">spider</span></em>, <em class="sig-param"><span class="n">errmsg</span><span class="o">=</span><span class="default_value">None</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter.download_error"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter.download_error" title="永久链接至目标">¶</a></dt>
<dd><p>记录来自spider的下载错误消息（通常来自引擎）。</p>
<div class="versionadded">
<p><span class="versionmodified added">2.0 新版功能.</span></p>
</div>
</dd></dl>

<dl class="py method">
<dt id="scrapy.logformatter.LogFormatter.dropped">
<code class="sig-name descname">dropped</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">item</span></em>, <em class="sig-param"><span class="n">exception</span></em>, <em class="sig-param"><span class="n">response</span></em>, <em class="sig-param"><span class="n">spider</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter.dropped"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter.dropped" title="永久链接至目标">¶</a></dt>
<dd><p>当某个项在通过项管道时被丢弃时，记录该消息。</p>
</dd></dl>

<dl class="py method">
<dt id="scrapy.logformatter.LogFormatter.item_error">
<code class="sig-name descname">item_error</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">item</span></em>, <em class="sig-param"><span class="n">exception</span></em>, <em class="sig-param"><span class="n">response</span></em>, <em class="sig-param"><span class="n">spider</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter.item_error"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter.item_error" title="永久链接至目标">¶</a></dt>
<dd><p>当一个项目在通过项目管道时出错时，记录消息。</p>
<div class="versionadded">
<p><span class="versionmodified added">2.0 新版功能.</span></p>
</div>
</dd></dl>

<dl class="py method">
<dt id="scrapy.logformatter.LogFormatter.scraped">
<code class="sig-name descname">scraped</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">item</span></em>, <em class="sig-param"><span class="n">response</span></em>, <em class="sig-param"><span class="n">spider</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter.scraped"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter.scraped" title="永久链接至目标">¶</a></dt>
<dd><p>当一个项目被蜘蛛抓取时记录一条消息。</p>
</dd></dl>

<dl class="py method">
<dt id="scrapy.logformatter.LogFormatter.spider_error">
<code class="sig-name descname">spider_error</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">failure</span></em>, <em class="sig-param"><span class="n">request</span></em>, <em class="sig-param"><span class="n">response</span></em>, <em class="sig-param"><span class="n">spider</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/logformatter.html#LogFormatter.spider_error"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.logformatter.LogFormatter.spider_error" title="永久链接至目标">¶</a></dt>
<dd><p>记录来自spider的错误消息。</p>
<div class="versionadded">
<p><span class="versionmodified added">2.0 新版功能.</span></p>
</div>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="advanced-customization">
<span id="topics-logging-advanced-customization"></span><h3>高级自定义<a class="headerlink" href="#advanced-customization" title="永久链接至标题">¶</a></h3>
<p>因为scrapy使用stdlib日志记录模块，所以可以使用stdlib日志记录的所有功能自定义日志记录。</p>
<p>例如，假设您正在抓取一个返回许多HTTP 404和500响应的网站，并且您希望隐藏像这样的所有消息：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">2016</span><span class="o">-</span><span class="mi">12</span><span class="o">-</span><span class="mi">16</span> <span class="mi">22</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">06</span> <span class="p">[</span><span class="n">scrapy</span><span class="o">.</span><span class="n">spidermiddlewares</span><span class="o">.</span><span class="n">httperror</span><span class="p">]</span> <span class="n">INFO</span><span class="p">:</span> <span class="n">Ignoring</span>
<span class="n">response</span> <span class="o">&lt;</span><span class="mi">500</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">quotes</span><span class="o">.</span><span class="n">toscrape</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">page</span><span class="o">/</span><span class="mi">1</span><span class="o">-</span><span class="mi">34</span><span class="o">/&gt;</span><span class="p">:</span> <span class="n">HTTP</span> <span class="n">status</span> <span class="n">code</span>
<span class="ow">is</span> <span class="ow">not</span> <span class="n">handled</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">allowed</span>
</pre></div>
</div>
<p>首先要注意的是一个记录器名称-它在括号中： <code class="docutils literal notranslate"><span class="pre">[scrapy.spidermiddlewares.httperror]</span></code> . 如果你得到公正 <code class="docutils literal notranslate"><span class="pre">[scrapy]</span></code> 然后 <a class="reference internal" href="settings.html#std-setting-LOG_SHORT_NAMES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LOG_SHORT_NAMES</span></code></a> 可能设置为true；设置为false并重新运行爬网。</p>
<p>接下来，我们可以看到消息具有信息级别。为了隐藏它，我们应该为 <code class="docutils literal notranslate"><span class="pre">scrapy.spidermiddlewares.httperror</span></code> 高于信息；信息后的下一级是警告。可以这样做，例如在蜘蛛的 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">scrapy</span>


<span class="k">class</span> <span class="nc">MySpider</span><span class="p">(</span><span class="n">scrapy</span><span class="o">.</span><span class="n">Spider</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;scrapy.spidermiddlewares.httperror&#39;</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">WARNING</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>如果您再次运行此蜘蛛，则从 <code class="docutils literal notranslate"><span class="pre">scrapy.spidermiddlewares.httperror</span></code> 日志记录器将消失。</p>
</div>
</div>
<div class="section" id="module-scrapy.utils.log">
<span id="scrapy-utils-log-module"></span><h2>scrapy.utils.log模块<a class="headerlink" href="#module-scrapy.utils.log" title="永久链接至标题">¶</a></h2>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="stats.html" class="btn btn-neutral float-right" title="统计数据集合" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="exceptions.html" class="btn btn-neutral float-left" title="例外情况" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2008–2020, Scrapy developers
      <span class="lastupdated">
        最后更新于 10月 18, 2020.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
 
<script type="text/javascript">
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";
analytics.load("8UDQfnf3cyFSTsM4YANnW5sXmgZVILbA");
analytics.page();
}}();

analytics.ready(function () {
    ga('require', 'linker');
    ga('linker:autoLink', ['scrapinghub.com', 'crawlera.com']);
});
</script>


</body>
</html>